package com.wbpay.sdk.dlg;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.wbpay.sdk.WbSdk;
import com.wbpay.sdk.utils.Utils;

/**
 * Created by ck01- on 2017/9/19.
 */

public class ExitDialog extends Dialog implements View.OnClickListener {
    private ImageView mExitImage;
    private WbSdk.OnExitCallback mOnExitCallback;

    public ExitDialog(Context context) {
        super(context);
        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }

    public void setOnExitCallback(WbSdk.OnExitCallback callback) {
        mOnExitCallback = callback;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(Utils.getLayoutId(getContext(), "jy_dialog_exit"));
        mExitImage = (ImageView) findViewById(Utils.getId(getContext(), "exit_image"));
        View exitOk = findViewById(Utils.getId(getContext(), "exit_ok"));
        View exitCancel = findViewById(Utils.getId(getContext(), "exit_cancel"));
        exitOk.setOnClickListener(this);
        exitCancel.setOnClickListener(this);
        updateWindow();
    }

    private void updateWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams a = window.getAttributes();
        DisplayMetrics dm = getContext().getResources().getDisplayMetrics();
        a.width = (int) (dm.widthPixels * 0.85f);
        window.setAttributes(a);
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == Utils.getId(getContext(), "exit_ok")) {
            dismiss();
            if (mOnExitCallback != null) {
                mOnExitCallback.onExitConfirm();
            }
        } else if (v.getId() == Utils.getId(getContext(), "exit_cancel")) {
            dismiss();
            if (mOnExitCallback != null) {
                mOnExitCallback.onExitCancel();
            }
        }
    }
}
